package com.xicheng.immerse

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.immerse.ext.ILogger
import com.xicheng.immerse.ui.theme.ImmerseTheme
import com.xicheng.immerse.viewmodel.MainViewModel
import com.xicheng.immerse.viewmodel.uistate.UiState
import com.xicheng.immerse.viewmodel.intent.ViewIntent

class MainActivity : ComponentActivity(), ILogger {
    companion object {
        private const val TAG = "MainActivity"
    }

    override val tag: String = TAG

    private val viewModel by viewModels<MainViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ImmerseTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    viewModel.state.value.NavState(viewModel::sendIntent)
                }
            }
        }
    }
}

@Composable
fun UiState.NavState(intent: (ViewIntent) -> Unit) {
    when (this) {
        is UiState.Loading -> {
            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Text(text = "Loading")
            }
        }


        else -> {}
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    ImmerseTheme {
        Greeting("Android")
    }
}